package pers.leeyns.rd.chinese;

import pers.leeyns.rd.base.RandomStringGenerator;
import pers.leeyns.rd.utils.DictionaryUtils;

/**
 * @description 中文生成
 * @author Leeyns
 * @date 2016年2月16日
 */
public class RandomChineseGenerator extends RandomStringGenerator {
	private static final long serialVersionUID = -8307255645964069997L;
    public static RandomChineseGenerator build(){
		return new RandomChineseGenerator();
	}
    private final static int delta = 0x9fa5 - 0x4e00 + 1;
    public String getRandomChinese(int number){
    	if(number > 0){
    		StringBuffer sbf = new StringBuffer();
    		for(int i = 0;i< number;i++){
    			sbf.append(getRandomChinese());
    		}
    		return sbf.toString();
    	}
    	return null;
    }
    
    public char getRandomString(){
    	return (char) (0x4e00 + this.nextInt(delta));
    }
    
    public String getRandomChinese(){
    	return getRandomString()+"";
    }
    
    public  String getRandomChineseByDictionary(int number){
    	if(number > 0){
    		StringBuffer sbf = new StringBuffer();
    		for(int i = 0;i< number;i++){
    			sbf.append(getRandomChineseByDictionary());
    		}
    		return sbf.toString();
    	}
    	return null;
    }
    
    public  String getRandomChineseByDictionary(){
    	String str = DictionaryUtils.getZHCNResource();
    	int radix = this.getRandomNumber(str.length());
    	return  str.substring(radix, radix+1);
    }
    
	 
	/**
	 * 随机从chineseRes 中获取Length 个中文字符
	 * @param chineseRes 元中文字符串
	 * @param length 返回的字符串的长度
	 * @return
	 */
	public String getRandomChinese(String chineseRes,int length){
		return this.getRandomString(chineseRes, length);
	}
	
	public static void main(String[] args) {
//		RandomChineseGenerator r = RandomChineseGenerator.build();
//		System.out.println(r.getRandomChinese("张莎薇娥我根本", 3));
	}
}
